package org.ph.share.lock.test.thread;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.TimeUnit;

/**
 * Thread 测试类
 * 泡茶demo
 *
 * @author Hush.
 * @since 2023/5/15 9:53
 */
@Slf4j
public class ThreadTest6 {
    public static void main(String[] args) throws InterruptedException {
        RunnableFuture<String> task1 = new FutureTask<>(() -> {
            log.info("tea 洗茶壶");
            TimeUnit.SECONDS.sleep(1);
            log.info("tea 洗茶杯");
            TimeUnit.SECONDS.sleep(2);
            log.info("tea 拿茶叶");
            TimeUnit.SECONDS.sleep(10);
            // log.info("泡茶时间....");
            return "龙井tea";
        });
        Thread thread1 = new Thread(task1,"t2");
        Thread thread = new Thread("t1") {
            @SneakyThrows
            @Override
            public void run() {

                log.info("tea - 1 洗水壶");
                TimeUnit.SECONDS.sleep(1);

                for (int i = 3; i <= 10; i++) {
                    TimeUnit.SECONDS.sleep(1);
                    log.info("tea - 2 开始烧水壶,度数:{}", i * 10);
                }
                // 如有必要，等待计算完成，然后检索其结果。这里会等待t1的执行结果
                log.info("拿到t2的茶叶:{}",task1.get());
                log.info("泡茶时间....");
            }
        };
        // Thread thread2 = new Thread("t2") {
        //     @SneakyThrows
        //     @Override
        //     public void run() {
        //         log.info("tea 洗茶壶");
        //         TimeUnit.SECONDS.sleep(1);
        //         log.info("tea 洗茶杯");
        //         TimeUnit.SECONDS.sleep(2);
        //         log.info("tea 拿茶叶");
        //         TimeUnit.SECONDS.sleep(1);
        //         // // 这边t2线程等待t1线程结束开始泡茶
        //         // thread.join();
        //         log.info("泡茶时间....");
        //     }
        // };
        thread.start();
        thread1.start();
    }
}
